/*
 * Middleware
 * Copyright (C) 2010/11
 * Marco Buzzanca, Davide Giuseppe Monaco, Riccardo Pulvirenti, Giuseppe Ravidà,
 * Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: file_cta_proxy.hpp
 */

#ifndef _FILE_CTA_PROXY_HPP_
#define _FILE_CTA_PROXY_HPP_

#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <fstream>
#include <iostream>

//-----------------------------------------------------------------------------
// Constants and enums
namespace middleware {
/*!
 * Port.
 */
typedef unsigned int CTAPort;
/*!
 * File open mode.
 */
typedef enum {
	/*!
	 * Every call to op<< appends the text. Default.
	 */
	kOpenAppend = 0,
	/*!
	 * Every call to op<< overwrite text.
	 */
	kOpenOverwrite = 1
} FileCTAOpenMode;
/*!
 * Protocol constant: ask for reading.
 */
const std::string kCTAProtocolReadRequest = "READ";
/*!
 * Protocol constant: read granted.
 */
const std::string kCTAProtocolReadGranted = "READ_OK";
/*!
 * Protocol constant: read denied.
 */
const std::string kCTAProtocolReadDenied = "READ_NO";
/*!
 * Protocol constant: done reading.
 */
const std::string kCTAProtocolReadDone = "READ_DONE";
} /* middleware */
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*!
 * Proxy for centralized token agreement file management.
 */
class FileCTAProxy {
private:
	/*!
	 * Name of the file.
	 */
    std::string _filename;
    /*!
     * Port.
     */
    CTAPort _port;
    /*!
	 * Open mode.
	 */
	FileCTAOpenMode _open_mode;
    /*!
     * Thread that will listen for requests by others.
     */
    boost::shared_ptr<boost::thread> _listener;
    /*!
     * Mutex for the resource to lock and unlock.
     */
    boost::mutex _write;
    /*!
     * Execution flow of listener.
     */
    void exec_listen();
public:
    /*!
     * Providing the filename.
     */
    FileCTAProxy(CTAPort port, const std::string& filename);
    /*!
	 * Providing the filename.
	 */
	FileCTAProxy(CTAPort port, const std::string& filename,
			FileCTAOpenMode open_mode);
    /*!
     * Destructor.
     */
    ~FileCTAProxy();
    /*!
     * Gets the filename.
     */
    std::string GetFilename(void) const {
    	return this->_filename;
    }
    /*!
     * Sets the filename.
     */
    void SetFilename(const std::string& filename) {
    	this->_filename = filename;
    }
    /*!
	 * Writing in file minding concurrency.
	 */
	FileCTAProxy& operator<<(const std::string& str);
}; /* FileCTAProxy */
} /* Middleware */

#endif
